package com.controller.buying 
{
	
	import com.model.valueObjects.VoInsertedSc;
	import com.model.valueObjects.VoPurchaseOrder;
	import com.model.valueObjects.VoSalesConformation;
	import com.model.valueObjects.VoScListForPO;
	import com.service.PurchaseOrder;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class PurchaseOrderController extends AbstractController
	{
		
		[Inject]
		public var purchaseOrderService:PurchaseOrder;
		
		public function PurchaseOrderController()
		{
			super();
		}
		
		[EventHandler(event="PurchaseOrderEvent.GET_ALL")]
		public function getAll():void{
			sh.executeServiceCall(purchaseOrderService.getAllPO(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{	
			if(model.currentMenu=='poBtn')model.currentBuyTableIndex=1;
				
			model.poHeaderList=event.result as ArrayCollection;
			model.poHeaderList.refresh();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="PurchaseOrderEvent.GET_ALL_UN_MAPPED_SC")]
		public function getAllUnMappedSc():void{
			sh.executeServiceCall(purchaseOrderService.getUnMappedIndentScList(),getAllUnMappedScResult, handleFault );
		}
		private function getAllUnMappedScResult(event:ResultEvent):void
		{					
			model.scUnMappedIndent = event.result as ArrayCollection;
		}
		
		[EventHandler(event="PurchaseOrderEvent.GET_PO_ITEMS" ,  properties="SupplierFk,grFk,edit")]
		public function getPoItems(SupplierFk:Number,grFk:Number,edit:Number):void{
			sh.executeServiceCall(purchaseOrderService.getItemsForGr(SupplierFk,grFk,edit),getPoItemsResult, handleFault );
		}
		private function getPoItemsResult(event:ResultEvent):void
		{
			model.poItemForGr=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
	/*	[EventHandler(event="PurchaseOrderEvent.GET_PRO_ITEMS" , properties="POSerialNo")]
		public function getProItems(po_sno:Number):void{
			sh.executeServiceCall(purchaseOrderService.getProductforPoGr(po_sno),getProItemsResult, handleFault );
		}
		private function getProItemsResult(event:ResultEvent):void
		{
			trace("read data");
			model.proItemForGr=event.result as ArrayCollection;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}*/
		
		[EventHandler(event="PurchaseOrderEvent.GET_PO_ITEMS_OTHER_SC" , properties="SupplierFk,grFk,edit")]
		public function getPoItemsOtherSc(SupplierFk:Number,grFk:Number,edit:Number):void{
			sh.executeServiceCall(purchaseOrderService.getItemsForGr_OtherSc(SupplierFk,grFk,edit),getPo_Sc_ItemsResult, handleFault );

		}
		private function getPo_Sc_ItemsResult(event:ResultEvent):void
		{
		
			model.poItemOtherScForGr=event.result as ArrayCollection;
			trace(">>>> "+model.poItemOtherScForGr.length);
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="PurchaseOrderEvent.ADD_AND_MAP", properties="purchaseOrder_vo")]
		public function addAndMap(purchaseOrder_vo:VoPurchaseOrder):void
		{
			sh.executeServiceCall(purchaseOrderService.addNewPOAndMapSc(purchaseOrder_vo), addAndMapResult, handleFault );	
		}
		private function addAndMapResult(event:ResultEvent):void
		{
			model.poCode=event.result as String;
			if(model.saveCloseBtn)model.currentBuyFormsIndex=0;
			
			Alert.show( "Purchase Order added" );		
			

			getAll();
			getAllUnMappedSc();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;	
		}
		
		[EventHandler(event="PurchaseOrderEvent.ADD_SC_FROM_PO", properties="scVo")]
		public function addScFromPo(scVo:VoSalesConformation):void
		{
			sh.executeServiceCall(purchaseOrderService.addNewScFromPO(scVo), addScFromPoResult, handleFault );	
		}
		private function addScFromPoResult(event:ResultEvent):void
		{	
			
			var aesc:VoInsertedSc = event.result as VoInsertedSc;
			
			for(var i:int=0;i<aesc.sc_detserialnumber_fk.length;i++)
			{
				for each(var selObj:Object in model.poFrmSelectedProducts)
				{
					if(Number(selObj.pro_serialnumber_fk)==Number(aesc.pro_serialnumber_fk[i]) && 
						Number(selObj.sc_detserialnumber)==0)
					{
						selObj.sc_detserialnumber = aesc.sc_detserialnumber_fk[i];
						selObj.sales_con_code = aesc.sales_con_code;
						break;
					}
				}						
			}
			
			model.poFrmSelectedProducts.refresh();
			Alert.show(aesc.sales_con_code + " - Created" );		
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;	
			model.poStackIndex = 0;
		}
		
		[EventHandler(event="PurchaseOrderEvent.UPDATE", properties="purchaseOrder_vo")]
		public function update(purchaseOrder_vo:VoPurchaseOrder):void
		{
			sh.executeServiceCall(purchaseOrderService.editPO(purchaseOrder_vo), editResult, handleFault );	
		}
		private function editResult(event:ResultEvent):void
		{
			model.poCode=event.result as String;
			getAll();
			if(model.saveCloseBtn)model.currentBuyFormsIndex=0;
			getAllUnMappedSc();
			Alert.show( "PurchaseOrder Updated" );	
		}
		
		[EventHandler(event="PurchaseOrderEvent.DELETE", properties="POSerialNo")]
		public function deletef(POSerialNo:Number):void
		{
			sh.executeServiceCall(purchaseOrderService.deletePO(POSerialNo),deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			model.currentBuyFormsIndex=0;	
			getAll();
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show( "PurchaseOrder Deleted" );	
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}